package cn.zxcoder.controller.SystemManage.department;

import cn.zxcoder.entity.SystemManage.Dept;
import cn.zxcoder.entity.Res;
import cn.zxcoder.service.IDeptService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 公司信息表 前端控制器
 * </p>
 *
 * @author zx
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/api/Dept")
@Api(tags = "部门管理的一些op")
public class DeptController {

    @Autowired
    IDeptService iDeptServiceIml;

    @ApiOperation("获取部门信息")
    @PostMapping("/list")
    @Transactional(readOnly = true)
    public Res getDept(@RequestParam String page, @RequestParam String limit,
                       @RequestParam(required =false) String deptName,@RequestParam(required =false) String deptCode) {
        IPage<Dept> deptPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        if (deptName.equals("") && deptCode.equals("")) {//都为空的时候
            IPage<Dept> page1 = iDeptServiceIml.page(deptPage);
            return new Res(true, JSON.toJSONString(page1), "成功获取部门信息");
        }
        else if (deptName.equals("")){//搜索信息种只有部门名字
            QueryWrapper<Dept> wrapper = new QueryWrapper<>();
            wrapper.like("code", deptCode);
            IPage<Dept> page1 = iDeptServiceIml.page(deptPage, wrapper);
            return new Res(true, JSON.toJSONString(page1), "成功获取部门信息");
        }
        else if (deptCode.equals("")) {//搜索信息种只有部门代码
            QueryWrapper<Dept> wrapper = new QueryWrapper<>();
            wrapper.like("name", deptName);
            IPage<Dept> page1 = iDeptServiceIml.page(deptPage, wrapper);
            return new Res(true, JSON.toJSONString(page1), "成功获取部门信息");
        }
        else {//搜索信息种名字和代码都有
            QueryWrapper<Dept> wrapper = new QueryWrapper<>();
            wrapper.like("name", deptName).and(i->i.like("code", deptCode));
            IPage<Dept> page1 = iDeptServiceIml.page(deptPage, wrapper);
            return new Res(true, JSON.toJSONString(page1), "成功获取部门信息");
        }

    }

    @ApiOperation("保存部门信息")
    @PostMapping("/save")
    @Transactional
    public Res saveDept(@RequestParam String deptName,@RequestParam String deptCode,
                        @RequestParam String token,@RequestParam boolean issave,@RequestParam String deptId) {

        if (issave) {//保存
            Dept dept = new Dept();
            dept.setCode(deptCode);
            dept.setName(deptName);
            dept.setEditUser(token);
            dept.setEditTime(new Date());

            boolean save = iDeptServiceIml.save(dept);
            return new Res(save, null, "保存部门信息成功");
        } else {//编辑
            UpdateWrapper<Dept> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("name", deptName);
            updateWrapper.set("code", deptCode);
            updateWrapper.set("editUser", token);
            updateWrapper.set("editTime", new Date());
            updateWrapper.eq("id", Integer.parseInt(deptId));
            boolean update = iDeptServiceIml.update(updateWrapper);
            return new Res(update, null, "编辑部门信息成功");
        }

    }

    @ApiOperation("删除部门信息")
    @GetMapping("/delete/{id}")
    @Transactional

    public Res deleteDept(@PathVariable String id) {
        boolean b = iDeptServiceIml.removeById(Integer.parseInt(id));
        return new Res(b, null, "删除部门信息成功");
    }

}
